import 'dart:convert';
import 'dart:io';

import 'package:convex_bottom_bar/convex_bottom_bar.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
// import 'package:flutter_native_splash/flutter_native_splash.dart';
import 'package:pax_info_app/config/constants.dart';
import 'package:pax_info_app/model/sys_info.dart';
import 'package:pax_info_app/ui/pages/clock/clock_list_page.dart';
import 'package:pax_info_app/ui/pages/clock/clock_page.dart';
import 'package:pax_info_app/ui/pages/setting/setting_page.dart';
import 'package:pax_info_app/utils/uidata.dart';
// import 'package:jpush_flutter/jpush_flutter.dart';

class TabControl extends StatefulWidget {
  final int userNo;
  final String fDate;
  TabControl(this.userNo, this.fDate);

  @override
  State createState() => TabControlState();
}

class TabControlState extends State<TabControl> {

  int _tabIndex = 0;

  var tabImages;
  var _body;
  var pages;
  final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();

  String? debugLable = 'Unknown';
  // final JPush jpush = new JPush();

  @override
  void initState() {
    super.initState();
    pages = <Widget>[
      ClockPage(widget.userNo, widget.fDate),
      ClockListPage(),
      SettingPage(),
    ];
    // initPlatformState();
    // FlutterNativeSplash.remove();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  // Future<void> initPlatformState() async {
  //   String? platformVersion;
  //
  //   try {
  //     jpush.addEventHandler(
  //         onReceiveNotification: (Map<String, dynamic> message) async {
  //           print("flutter onReceiveNotification: $message");
  //           setState(() {
  //             debugLable = "flutter onReceiveNotification: $message";
  //           });
  //           String extraStr = Platform.isIOS ? '' : message['extras']['cn.jpush.android.EXTRA'];
  //           String content = Platform.isIOS ?
  //           message['aps']['alert'] : message['alert'];
  //
  //           print('>>>>>>>>> content: $content');
  //
  //           Map extras = Platform.isIOS ? message['extras'] : jsonDecode(extraStr);
  //
  //           print('>>>>>>>>> extras: $extras');
  //
  //           // $mer.Message msg = new $mer.Message(
  //           //     icon: extras['icon'],
  //           //     title: message['title'],
  //           //     content: content,
  //           //     msgClass: extras['msgClass'],
  //           //     msgType: extras['msgType'],
  //           //     isRead: 0,
  //           //     createTime: formatDate(DateTime.now(), [yyyy, '-', mm, '-', dd, ' ', HH, ':', nn, ':', ss])
  //           // );
  //           //
  //           // var db = new DatabaseHelper();
  //           // int i = await db.saveMessage(msg);
  //           // _messageCountModel.initData();
  //         }, onOpenNotification: (Map<String, dynamic> message) async {
  //       print("flutter onOpenNotification: $message");
  //       setState(() {
  //         debugLable = "flutter onOpenNotification: $message";
  //       });
  //     }, onReceiveMessage: (Map<String, dynamic> message) async {
  //       print("flutter onReceiveMessage: $message");
  //       setState(() {
  //         debugLable = "flutter onReceiveMessage: $message";
  //       });
  //     }, onReceiveNotificationAuthorization:
  //         (Map<String, dynamic> message) async {
  //       print("flutter onReceiveNotificationAuthorization: $message");
  //       setState(() {
  //         debugLable = "flutter onReceiveNotificationAuthorization: $message";
  //       });
  //     });
  //   } on PlatformException {
  //     platformVersion = 'Failed to get platform version.';
  //   }
  //
  //   jpush.setup(
  //     appKey: Constants.jpushAppKey, //你自己应用的 AppKey
  //     channel: "developer-default",
  //     production: false,
  //     debug: true,
  //   );
  //   jpush.setAlias(SysInfo.instance.userName.toString());
  //   jpush.applyPushAuthority(
  //       new NotificationSettingsIOS(sound: true, alert: true, badge: true));
  //
  //   // Platform messages may fail, so we use a try/catch PlatformException.
  //   jpush.getRegistrationID().then((rid) {
  //     print("flutter get registration id : $rid");
  //     setState(() {
  //       debugLable = "flutter getRegistrationID: $rid";
  //     });
  //   });
  //
  //   // If the widget was removed from the tree while the asynchronous platform
  //   // message was in flight, we want to discard the reply rather than calling
  //   // setState to update our non-existent appearance.
  //   if (!mounted) return;
  //
  //   setState(() {
  //     debugLable = platformVersion;
  //   });
  // }

  void tabTo(int idx) {
    setState(() {
      _tabIndex = idx;
    });
  }

  @override
  Widget build(BuildContext context) {

    // _body = IndexedStack(
    //   children: pages,
    //   index: _tabIndex,
    // );
    /// 每次切换标签时需要刷新页面数据
    _body = pages[_tabIndex];

    return Scaffold(
      key: _scaffoldKey,
      body: WillPopScope(
          child: _body,
          onWillPop: () {
//            NativeTest.backDesktop();
            return Future.value(true);
          }),
      bottomNavigationBar: ConvexAppBar(
        backgroundColor: UIData.primaryColor,
        items: [
          TabItem(icon: Icons.watch_later_outlined, title: '打卡'),
          TabItem(icon: Icons.list_alt_outlined, title: '统计'),
          TabItem(icon: Icons.settings_outlined, title: '设置'),

        ],
        initialActiveIndex: 0,//optional, default as 0
        onTap: (int i) => tabTo(i),
      ),
    );
  }
}